package com.servlet;

import com.entity.Students;
import com.tools.Utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Insert {
    public boolean insert(Students students){
        Connection con=Utils.getConn();
        //构造一个sql，？是占位符
        String sql="INSERT INTO students(sname,sage,sclass) VALUES (?,?,?);";
        try {
            //用连接对象构造一个预加载sql执行器
            PreparedStatement pstate=con.prepareStatement(sql);
            //用传来的实体对象填充sql占位符
            pstate.setString(1,students.getSname());
            pstate.setString(2,students.getSage());
            pstate.setString(3,students.getSclass());
            //sql执行器预加载专用的sql执行方法
            pstate.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }
}
